//创建节点绑定事件
var x = false; //用户名
var y = false; //密码
var z = false; //重复密码
var t = false; //手机号码验证
var e = false; //邮箱验证
var h = false; //用户资料信息

//1.用户名判断
getEle("#username").onfocus = function() {
    getEle(".usernamemsg").style.display = "block"
};
// 用户名仅支持中文、 字母、 数字、“ - ”“_” 的组合， 4 - 20 个字符
var reg1 = /^[\w\-\u4E00-\u9FA5]{6,20}$/;
getEle("#username").onblur = function() {
    // console.log(this.value);
    let valname = this.value;
    if (reg1.test(valname)) {
        getEle("#username_msg").innerHTML = "✔"
        getEle("#username_msg").style.fontSize = 20;
        getEle(".usernamemsg").style.display = "none"
        x = true
    } else {
        getEle("#username_msg").innerHTML = ""
        getEle(".usernamemsg").style.display = "block"
        x = false
    }
    setTimeout(function() { getEle(".usernamemsg").style.display = "none" }, 3000);
}

//2.密码判断
getEle("#userpass").onfocus = function() {
    getEle(".userpassmsg").style.display = "block"
};
getEle("#userpass").onblur = function() {
    let valpass = this.value;
    if (valpass.length >= 6 && valpass.length <= 20) {
        let a = 0,
            b = 0,
            c = 0;
        let reg1 = /\d+/;
        a = reg1.test(valpass) ? 1 : 0;
        var reg2 = /[a-zA-Z\d]+/;
        b = reg2.test(valpass) ? 1 : 0;
        var reg3 = /\w+/;
        c = reg3.test(valpass) ? 1 : 0;
        //给强度做出判断
        let str = '';
        switch (a + b + c) {
            case 1:
                str = '弱'
                break;
            case 2:
                str = '中'
                break;
            case 3:
                str = '强'
                break;
        }
        getEle("#userpass_msg").innerHTML = str;
        getEle(".userpassmsg").style.display = "none"
        y = true;
    } else {
        getEle("#userpass_msg").innerHTML = '密码长度不够';
        y = false;
    }
    setTimeout(function() { getEle(".userpassmsg").style.display = "none" }, 3000);
}

//3.验证重复密码
getEle("#user2pass").onblur = function() {
    let passtwo = this.value;
    let passone = getEle("#userpass").value;
    if (passtwo === passone) {
        getEle("#user2pass_msg").innerHTML = '密码一致';
        z = true;
    } else {
        getEle("#user2pass_msg").innerHTML = '密码不一致';
        z = false;
    }
}


//3.邮箱判断
getEle("#usreemail").onfocus = function() {
    getEle(".usreemailmsg").style.display = "block"
};
var reg2 = /^\w{3,15}@{1}[\da-z]{2,9}\.{1}(com|cn|net){1}$/i;
getEle("#usreemail").onblur = function() {
    let valemail = this.value;
    if (reg2.test(valemail)) {
        getEle("#usreemail_msg").innerHTML = "✔"
        getEle("#usreemail_msg").style.fontSize = 20;
        getEle(".usreemailmsg").style.display = "none"
        e = true
    } else {
        getEle("#usreemail_msg").innerHTML = ""
        getEle(".usreemailmsg").style.display = "block"
        e = false
    }
    setTimeout(function() { getEle(".usreemailmsg").style.display = "none" }, 3000);
}

//4.手机号判断
getEle("#usrphone").onfocus = function() {
    getEle(".usrphonemsg").style.display = "block"
};
var reg3 = /^1[356789]{1}\d{9}$/;
getEle("#usrphone").onblur = function() {
    let valphone = this.value;
    if (reg3.test(valphone)) {
        getEle("#usrephone_msg").innerHTML = "✔"
        getEle("#usrephone_msg").style.fontSize = 20;
        getEle(".usrphonemsg").style.display = "none"
        t = true
    } else {
        getEle("#usrephone_msg").innerHTML = ""
        getEle(".usrphonemsg").style.display = "block"
        t = false
    }
    setTimeout(function() { getEle(".usrphonemsg").style.display = "none" }, 3000);
}




getEle("#zcbutton").disabled = true;


getEle("#msg_logo").onclick = function() {
    if (x == true && y == true && z == true && t == true && e == true) {
        getEle(".bottom_button_logo").style.display = "block";
        getEle("#zcbutton").style.backgroundColor = "#e31d3b"
        getEle("#zcbutton").disabled = false;
        alert("用户信息输入完成")
    } else {
        alert("还有信息未完善")
    }
}


$("#zcbutton").click(
    (eve) => {
        // 取消点击提交的默认行为
        eve.preventDefault();
        // 发送ajax请求
        $.ajax({
            url: "http://localhost:3000/api",
            data: {
                type: "register",
                username: $("#username").val(),
                password: $("#userpass").val()
            },
            success: (res) => {
                if (JSON.parse(res).code === 1) {
                    alert(JSON.parse(res).title);
                    location.href = "http://localhost:3000/www/login.html";
                }
            }
        })
    }
)






















//获取节点的方法
function getEle(tag) {
    return document.querySelector(tag)
}